<?php

namespace app\tool\invite;

use app\factory\Factory;
use app\factory\model\RequestAgreement;
use app\model\invite\InviteModel;
use app\modelOpreate\invite\Invite;
use app\tool\ToolBase;

/**
 * ClassName:发布帖子处理器
 * @author leijunxiang
 * @time 2024/10/31
 * @auditor
 */
class PublishInviteHandler extends ToolBase
{
    const TOOL_PUBLISH_INVITE_HANDLER = "PublishInviteHandler";
    //标题
    protected $title;
    //内容
    protected $content;
    //图片队列
    protected $pictureList;
    //图片链接对列
    protected $pictureLinkList;
    //发帖人名称
    protected $publishName;
    //发贴人id
    protected $publishId;
    //发帖头像
    protected $publishIcon;
    //帖子M层对象
    protected $inviteModelObj;


    /**
     * 开始发布（请求对象）
     */
    public function startPublish($request)
    {
        //初始化数据
        $this->initData($request);
        //生成帖子M层对象
        $this->createInviteModelObj();
        //初始化相应属性
        $this->initParam();
        //发布帖子
        $this->publishInvite();
        //清除数据
        $this->cleanData();

    }

    /**
     * 初始化数据
     */
    public function initData($request)
    {
        $this->init();
        //获取请求数据
        $model = Factory::getInstance()->getModelObj(RequestAgreement::MODEL_REQUEST_AGREEMENT);
        $content = $model->getContent();
        if ($content && $request) {
            foreach ($content as $key => $value) {
                if (property_exists($this, $key)) {
                    $this->$key = $value;
                }
            }
            //请求图片链接
            $file = $request->file();
            $this->pictureList = $file['pictureList'];
            if (empty($this->title) || empty($this->content) || empty($this->publishId)) {
                $this->errCode = FAIL;
                $this->errMsg = "缺少必要参数，请重试!";
            }
            try {
                $fileSize = 1024 * 1024 * 2;
                validate(['pictureList'=>'fileSize:'.$fileSize.'|fileExt:jpg,png'])
                    ->check($file);
            } catch (\think\exception\ValidateException $e) {
               $this->errCode = FAIL;
               $this->errMsg  = $e->getMessage();
            }

        } else {
            $this->errCode = FAIL;
            $this->errMsg = "缺少必要参数，无法处理！";
        }
    }

    /**
     * 生成帖子M层对象
     */
    public function createInviteModelObj()
    {
        if (!$this->isGoOn()) {
            return;
        }
         $this->inviteModelObj = InviteModel::getObj();
    }

    /**
     * 初始化相应属性
     */
    public function initParam()
    {
        if (!$this->isGoOn()) {
            return;
        }
        //标题
        $title = $this->title;
        $this->inviteModelObj->setTitle($title);
        //内容
        $content = $this->content;
        $this->inviteModelObj->setContent($content);
        //生成图片链接对列
        $this->createPictureLinkList();
        //图片链接队列
        $pictureLinkList = $this->pictureLinkList;
        $this->inviteModelObj->setPictureLinkList($pictureLinkList);
//        //发帖人名称
//        $publishName = $this->publishName;
//        $this->inviteModelObj->setPublishName($publishName);
        //发贴人id
        $publishId = $this->publishId;
        $this->inviteModelObj->setPublishId($publishId);
//        //发帖头像
//        $publishIcon = $this->publishIcon;
//        $this->inviteModelObj->setPublishIcon($publishIcon);
        //发布时间
        $publishTime = time();
        $this->inviteModelObj->setPublishTime($publishTime);
        //点赞人数
        $likeNumber = 0;
        $this->inviteModelObj->setLikeNumber($likeNumber);
        //评论数
        $commentNumber = 0;
        $this->inviteModelObj->setCommentNumber($commentNumber);
        //观看人数
        $viewNumber = 0;
        $this->inviteModelObj->setViewNumber($viewNumber);
        //所属圈子
        $belongCircle = "";
        $this->inviteModelObj->setBelongCircle($belongCircle);
        //生成中文缩略
        $chineseAbbre = $this->createChineseAbbre($content);
        $this->inviteModelObj->setContentAbbre($chineseAbbre."...");
        //生成热门权重
        $hotWeight = 0;
        $this->inviteModelObj->setHotWeight($hotWeight);
    }

    /**
     * 发布帖子
     */
    public function publishInvite()
    {
        if (!$this->isGoOn()) {
            return;
        }
        //（true/false）发布帖子
        $flag = $this->inviteModelObj->publishInvite();
        if (!$flag) {
            $this->errCode = FAIL;
            $this->errMsg = "发布失败，请重试！";
        }
    }

    /**
     * 清除数据
     */
    public function cleanData()
    {
        //标题
        $this->title = "";
        //内容
        $this->content = "";
        //图片队列
        $this->pictureList = "";
        //图片链接对列
        $this->pictureLinkList = "";
        //发帖人名称
        $this->publishName = "";
        //发贴人id
        $this->publishId = "";
        //发帖头像
        $this->publishIcon = "";
        //帖子M层对象
        $this->inviteModelObj = "";

    }

    /**
     * 生成图片链接对列
     */
    public function createPictureLinkList()
    {
        if ($this->pictureList) {
            foreach ($this->pictureList as $picture) {
                $putFile = \think\facade\Filesystem::disk('public')->putFile('/', $picture);
                // 将反斜杠替换为正斜杠
                $putFile = str_replace('\\', '/', $putFile);
                $this->pictureLinkList[] = FORUM_IP . "/public/storage" . '/' . $putFile;
            }
            if($this->pictureLinkList&&is_array($this->pictureLinkList)){
                $this->pictureLinkList = json_encode($this->pictureLinkList);
            }
        }
    }

    /**
     * 生成中文缩略
     */
    public function createChineseAbbre($str)
    {
        $length = mb_strlen($str, 'utf-8'); // 获取中文字符串长度
        if ($length > 30) {
            return mb_substr($str, 0, 30, 'utf-8'); // 截取前30个中文字
        } else {
            return $str; // 字符串长度不超过30，直接返回
        }
    }


}